using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;

namespace MoEngine
{
    public class MasterWindowGame : MasterWindow
    {
        /// <summary>
        /// Background color
        /// </summary>

        public string Title
        {
            get { return m_Game.Window.Title; }
            set { m_Game.Window.Title = value; }
        }

        protected GraphicsDeviceManager m_GraphicsManager;
        
        protected GameCore m_Game;

        public MasterWindowGame(GameCore game)
        {
            m_Game = game;

            // Set graphics
            m_GraphicsManager = new GraphicsDeviceManager(game);

            // Set minimum requirements
            m_GraphicsManager.MinimumPixelShaderProfile = ShaderProfile.PS_2_0;
            m_GraphicsManager.MinimumVertexShaderProfile = ShaderProfile.VS_2_0;

#if DEBUG
            // Disable vertical retrace to get highest framerates possible for
            // testing performance.
            m_GraphicsManager.SynchronizeWithVerticalRetrace = false;
#endif
            m_GraphicsManager.PreferredBackBufferWidth = 1280;
            m_GraphicsManager.PreferredBackBufferHeight = 960;
            m_GraphicsManager.IsFullScreen = false;

            Title = "Core Game Window";

        }


        /// <summary>
        /// Graphics device reset handler.
        /// </summary>
        void graphics_DeviceReset(object sender, EventArgs e)
        {
            // Re-Set device
            // Restore z buffer state
            Device.RenderState.DepthBufferEnable = true;
            Device.RenderState.DepthBufferWriteEnable = true;
            // Set u/v addressing back to wrap
            Device.SamplerStates[0].AddressU = TextureAddressMode.Wrap;
            Device.SamplerStates[0].AddressV = TextureAddressMode.Wrap;
            // Restore normal alpha blending
            //SetCurrentAlphaMode(GameCore.AlphaMode.Default);

            // Set 128 and greate alpha compare for Model.Render
            Device.RenderState.ReferenceAlpha = 128;
            Device.RenderState.AlphaFunction = CompareFunction.Greater;

        }

        public override void  Initialize()
        {
            // Update resolution if it changes
            m_GraphicsManager.DeviceReset += new EventHandler(graphics_DeviceReset);
            m_Device = m_GraphicsManager.GraphicsDevice;
            graphics_DeviceReset(null, EventArgs.Empty);
        }

        public override void ValidateLayout()
        {
            //if (IsLayoutDirty)
            //{
            //    Rectangle clientRect = GameCore.Game.Window.ClientBounds;
            //    Layout.CalculateLayout(new Vector2(clientRect.Width, clientRect.Height));
                


            //    IsLayoutDirty = false;

            //    OnLayoutValidated();
            //}
        }

        public override Vector2 Size
        {
            get 
            {
                Rectangle clientRect = m_Game.Window.ClientBounds;
                return new Vector2(clientRect.Width, clientRect.Height);
            }
        }
    }
}
